<?php
class ssl_client_hello
{
    private $content;
    public $unpacked;
    public function ssl_client_hello($ctnt)
    {
        $this->content = $ctnt;
        $this->unpacked = array();
    }

    private function getCipher(&$nb_cipher)
    {
        $unpacked = unpack("H88/h4cipher_length",$this->content);
        $nb_cipher = hexdec($unpacked['cipher_length']);
        $str = "";
        for($i=0;$i<($nb_cipher/2);$i++)
        {
            $str.="H4cipher-suites".$i."/";
        }
        return $str;
    }

    private function getCompression($nb_cipher,&$nb_comp)
    {
        //we don't take : 88 same length as before
        //                4 (the cipher length)
        //                2*$nb_cipher -> 2 octets per cipher
        $nb_before = 88+4+2*$nb_cipher;
        $unpacked = unpack("H".$nb_before."/H2compression_length",$this->content);
        $nb_comp = hexdec($unpacked['compression_length']);
        $str = "";
        for($i=0;$i<($nb_comp);$i++)
        {
            $str.="H2compression".$i."/";
        }
        return $str;
    }

    private function getExtensions($nb_cipher,$nb_comp)
    {
        $nb_before = 88+4+2*$nb_cipher+2+2*$nb_comp;
        $unpacked = unpack("H".$nb_before."/H4extensions_length",$this->content);
        $nb_extensions = hexdec($unpacked['extensions_length']);
        $str = "";
        for($i=0;$i<($nb_comp);$i++)
        {
            $str.="H2compression".$i."/";
        }
        return $str;
    }

    private function getExtension()
    {
        //not really usefull
    }

    public function recup_client_hello()
    {
        $str_cipher = $this->getCipher($nb_cipher);
        $str_compre = $this->getCompression($nb_cipher,$nb_comp);

        //hexdec pour recuperer le contenu en decimal...
        $unpacked = unpack(
        "H2handshake/".
        "H4version/".
        "H4length/".
        /*HANDSHAKE PROTOCOLE CLIENT HELLO*/
            "H2type_hand/".
            "H6lengt_hand/".
            "H4version_hand/".
            /*RANDOM*/
                "H8gmt_unix_time/".
                "H56random/".
            "H2session_id_hand/".
            "H4cipher-suites-length_hand/".
            /*length/2 CIPHER SUITES*/
                $str_cipher.
            "H2compression_method_length/".
            /*COMPRESSION*/
                $str_compre.
            "H4extensions_length/".
        "c*",$this->content);
        $this->unpacked = $unpacked;
        return $unpacked;
    }
}

class ssl_server_hello
{
    function ssl_server_hello()
    {

    }

    function setServerHello()
    {
        $data = //HANDSHAKE
   "16".
   //TLS VERSION
   "0301".
   // LONGUEUR
   "0034".
   //SERVER HELLO
   "02".
   //LONGUEUR
   "000030".
   //TLS VERSION
   "0301".
   //TIME
   //"494e437b".
   dechex(time()).
   //random bytes -> to generate
   "e26d8d01f5e6b561fddf0f6989a7bb658ec56b63c0646f86b30983ad".
   //session id length
   "00".
   //CIPHER
   "0005".
   //Compression
   "00".
    //extensions length
    "0008".
    //server name
    "00000000".
    //session tickets
    "00230000";
    return pack(/*"H2H4H4H2H6H4H8H56H2H4H2H4H8H8"*/"H*",$data);

    }
}
?>